﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MapEditor.Model
{
    public class Rect
    {
        public double X;
        public double Y;
        public double Width;
        public double Height;

        public double getLeft() { return X - Width / 2; }
        public double getRight() { return X + Width / 2; }
        public double getTop() { return Y + Height / 2; }
        public double getBottom() { return Y - Height / 2; }

        public Rect() { }
        public Rect(double X, double Y, double Width, double Height)
        {
            this.X = X;
            this.Y = Y;
            this.Width = Width;
            this.Height = Height;
        }

        public bool IsIntersectOne(Rect A, Rect B)
        {
            double left, right, top, bottom;
            left = A.getLeft() < B.getLeft() ? B.getLeft() : A.getLeft();
            right = A.getRight() < B.getRight() ? A.getRight() : B.getRight();
            top = A.getTop() < B.getTop() ? A.getTop() : B.getTop();
            bottom = A.getBottom() < B.getBottom() ? B.getBottom() : A.getBottom();

            if (left < right && bottom < top)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Check inresect
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        /// <returns></returns>
        public bool IsIntersectAll(Rect A, Rect B)
        {
            return IsIntersectOne(A, B);
        }

        public bool CheckAABB(Rect A, Rect B)
        {
            return A.getRight() < B.getLeft() ||
                A.getLeft() > B.getRight() ||
                A.getBottom() < B.getTop() ||
                A.getTop() > B.getBottom();
        }
        
        public override string ToString()
        {
            return this.Width.ToString() + " " + this.Height.ToString() + " " + this.X.ToString() + " " + this.Y.ToString();
        }
    }
}
